home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / css.js < prev    next >
Encoding:
Text File  |  2007-11-11  |  2.6 KB  |  132 lines

  1. /*
  2.     css.js
  3.  
  4.     Copyright ┬⌐ 2005, 2006, 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. const WOT_STYLESHEET = "chrome://wot/skin/wot.css";
  8.  
  9. var wot_css =
  10. {
  11.     init: function()
  12.     {
  13.         try {
  14.             if (this.cache) { /* Window-specific cache */
  15.                 return;
  16.             }
  17.  
  18.             this.cache = new Object(); /* Rules */
  19.         } catch (e) {
  20.             dump("wot_css.init: failed with " + e + "\n");
  21.         }
  22.     },
  23.  
  24.     /* Finds a given rule from a stylesheet, caches found entries */
  25.     getstyle: function(href, id)
  26.     {
  27.         try {
  28.             var rule = this.cache[href + "." + id];
  29.  
  30.             if (rule) {
  31.                 return rule.style;
  32.             }
  33.  
  34.             var sheet;
  35.  
  36.             for (var i = 0; i < document.styleSheets.length; ++i) {
  37.                 sheet = document.styleSheets.item(i);
  38.  
  39.                 if (sheet.href != href) {
  40.                     continue;
  41.                 }
  42.  
  43.                 for (var j = 0; j < sheet.cssRules.length; ++j) {
  44.                     rule = sheet.cssRules.item(j);
  45.  
  46.                     if (rule.selectorText.indexOf(id) < 0) {
  47.                         continue;
  48.                     }
  49.  
  50.                     this.cache[href + "." + id] = rule;
  51.                     return rule.style;
  52.                 }
  53.                 break;
  54.             }
  55.         } catch (e) {
  56.             dump("wot_css.getstyle: failed with " + e + "\n");
  57.         }
  58.  
  59.         return null;
  60.     },
  61.  
  62.     /* Parses a numeric entry from a style rule, ignores units */
  63.     getstyle_numeric: function(style, parameter)
  64.     {
  65.         try {
  66.             if (style) {
  67.                 var value = style[parameter];
  68.  
  69.                 if (value) {
  70.                     var m = /^(\d+)/.exec(value);
  71.  
  72.                     if (m && m[1]) {
  73.                         return new Number(m[1]);
  74.                     }
  75.                 }
  76.             }
  77.         } catch (e) {
  78.             dump("wot_css.getstyle_numeric: failed with " + e + "\n");
  79.         }
  80.  
  81.         return null;
  82.     },
  83.  
  84.     /* Sets a numeric entry with given unit to the style rule */
  85.     setstyle_numeric: function(style, parameter, value, unit)
  86.     {
  87.         try {
  88.             style[parameter] = value + unit;
  89.         } catch (e) {
  90.             dump("wot_css.setstyle_numeric: failed with " + e + "\n");
  91.         }
  92.     },
  93.  
  94.     /* Parses a rect entry from a style rule, ignores units */
  95.     getstyle_rect: function(style, parameter)
  96.     {
  97.         try {
  98.             if (style) {
  99.                 var value = style[parameter];
  100.  
  101.                 if (value) {
  102.                     var r = /rect\(\s*(\d+)\D*,\s*(\d+)\D*,\s*(\d+)\D*,\s*(\d+)\D*\s*\)/;
  103.                     var m = r.exec(value);
  104.  
  105.                     if (m && m[1] && m[2] && m[3] && m[4]) {
  106.                         return new Array(Number(m[1]), Number(m[2]),
  107.                             Number(m[3]), Number(m[4]));
  108.                     }
  109.                 }
  110.             }
  111.         } catch (e) {
  112.             dump("wot_css.getstyle_rect: failed with " + e + "\n");
  113.         }
  114.  
  115.         return null;
  116.     },
  117.  
  118.     /* Sets a rect entry to the style rule, assumes pixels as unit */
  119.     setstyle_rect: function(style, parameter, rect)
  120.     {
  121.         try {
  122.             style[parameter] = "rect(" +
  123.                 rect[0].toFixed() + "px, " + rect[1].toFixed() + "px, " +
  124.                 rect[2].toFixed() + "px, " + rect[3].toFixed() + "px)";
  125.         } catch (e) {
  126.             dump("wot_css.setstyle_rect: failed with " + e + "\n");
  127.         }
  128.     }
  129. };
  130.  
  131. wot_css.init();
  132.